﻿//https://leetcode.cn/problems/product-of-array-except-self/

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums)
    {
        int n = nums.size();
        vector<int> f(n);
        f[0] = 1;
        //除nums[i]外数组的乘积
        for (int i = 1; i < n; i++)
        {
            f[i] = f[i - 1] * nums[i - 1];
        }

        vector<int> ret(n);
        int right = 1; //空间优化--记录该位置左边的乘积和
        for (int i = n - 1; i >= 0; i--)
        {
            ret[i] = f[i] * right;
            right *= nums[i];
        }
        return ret;
    }
};